//
//  BPOrderViewController.m
//  BabyPhotograph
//
//  Created by cesh on 2017/1/13.
//  Copyright © 2017年 cesh. All rights reserved.
//

#import "BPOrderViewController.h"
#import "BPOrderTableViewCell.h"
#import "BPChoosePicViewController.h"
#import "BPRefundViewController.h"
#import "BPPaySelectView.h"
#import "BPChosenPicsViewController.h"
#import "BPSubmitCommentViewController.h"
@interface BPOrderViewController () <UITableViewDelegate, UITableViewDataSource, BPOrderTableViewCellProtocal>

@property (strong, nonatomic) NSArray *data;

@end

@implementation BPOrderViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.alpha = 1.0;
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.alpha = 1.0;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的订单";
    self.view.backgroundColor = [UIColor colorWithRed:0xEC / 255.0 green:0xF5 / 255.0 blue:0xFC / 255.0 alpha:1.0];
    
    [self getData];
    
}

- (void)getData {
    [SVProgressHUD showWithStatus:@"读取中..."];
    NSDictionary *user = [[NSUserDefaults standardUserDefaults]objectForKey:@"user"];
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                     @"text/html",
                                                     @"image/jpeg",
                                                     @"image/png",
                                                     @"application/octet-stream",
                                                     @"text/json",
                                                     nil];
    [mgr POST:[NSString stringWithFormat:@"%@?c=Order&&a=myOrderList",requestURL]
   parameters:@{@"token" : user[@"token"],
                @"page" : @"1",
                @"pagesize" : @"10"
                }
     progress:^(NSProgress * _Nonnull uploadProgress) {
         
     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         if ([responseObject[@"state"] isEqualToString:@"SUCCESS"]) {
             [SVProgressHUD dismiss];
             self.data = responseObject[@"data"];
             NSLog(@"我的订单%@", self.data);
             if (self.data.count == 0) {
                 UIImageView *centerImgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"noOrder"]];
                 centerImgV.center = CGPointMake(screenW * 0.5, (screenH - 64) * 0.5);
                 [self.view addSubview:centerImgV];
                 
                 UILabel *label = [[UILabel alloc] init];
                 label.text = @"暂无订单";
                 label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
                 label.textColor = [UIColor colorWithRed:139/255.0 green:163/255.0 blue:182/255.0 alpha:1/1.0];
                 [label sizeToFit];
                 label.center = CGPointMake(screenW * 0.5, centerImgV.bottom + 10 + label.height * 0.5);
                 [self.view addSubview:label];
             }
             else {
                 UITableView *orderTableView = [[UITableView alloc]init];
                 orderTableView.backgroundColor = [UIColor colorWithRed:0xEC / 255.0 green:0xF5 / 255.0 blue:0xFC / 255.0 alpha:1.0];
                 orderTableView.frame = CGRectMake(0, 10 + 64, screenW, screenH - 10 - 64);
                 orderTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
                 [self.view addSubview:orderTableView];
                 
                 orderTableView.delegate = self;
                 orderTableView.dataSource = self;
             }
         }
         else {
             [SVProgressHUD showInfoWithStatus:@"请求错误"];
             dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                 [SVProgressHUD dismiss];
             });
         }
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         [SVProgressHUD showInfoWithStatus:@"网络故障"];
         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             [SVProgressHUD dismiss];
         });
     }];
}

#pragma mark - order table view datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BPOrderTableViewCell *cell = (BPOrderTableViewCell *)[[[NSBundle mainBundle]loadNibNamed:@"BPOrderTableViewCell" owner:nil options:nil]lastObject];
    
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //获取当前cell的数据
    NSDictionary *dellData = self.data[indexPath.row];
    cell.data = dellData;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 253;
}

#pragma mark - BPOrderTableViewCellProtocal 代理方法

- (void)pushViewController:(OrderCellType)orderCellType orderID:(NSString *)orderID{
    switch (orderCellType) {
            //1.待支付
        case OrderCellTypeNotPay:
        {
            //去支付
            BPPaySelectView *view = [[[NSBundle mainBundle]loadNibNamed:@"BPPaySelectView" owner:nil options:nil]lastObject];
            view.frame = [UIScreen mainScreen].bounds;
            [self.view addSubview:view];
        }
            break;
            //2.已支付（还没拍 等摄影师拍）
        case OrderCellTypeWaitForPhotographer:
        {
            //退款
            BPRefundViewController *refundVC = [[BPRefundViewController alloc]init];
            [self.navigationController pushViewController:refundVC animated:YES];
        }
            break;
            //3.已支付 (拍了 等传片)
        case OrderCellTypeWaitForChoosePics:
        {
            //不可操作
        }
            break;
            //4.已传片
        case OrderCellTypeGoChoosePics:
        {
            //去选片
            BPChoosePicViewController *choosePicVC = [[BPChoosePicViewController alloc]init];
            choosePicVC.orderID = orderID;
            [self.navigationController pushViewController:choosePicVC animated:YES];
        }
            break;
            //5.已选片
        case OrderCellTypeHaveChosenPics:
        {
            //查看选片
            BPChosenPicsViewController *chosenPicVC = [[BPChosenPicsViewController alloc]init];
            chosenPicVC.orderID = orderID;
            [self.navigationController pushViewController:chosenPicVC animated:YES];
        }
            break;
            //6.已完成
        case OrderCellTypeComplete:
        {
            //查看元件
            BPChosenPicsViewController *chosenPicVC = [[BPChosenPicsViewController alloc]init];
            chosenPicVC.orderID = orderID;

            [self.navigationController pushViewController:chosenPicVC animated:YES];
        }
            break;
            //7.已评价
        case OrderCellTypeSubmitComment:
        {
            //查看元件
            BPChosenPicsViewController *chosenPicVC = [[BPChosenPicsViewController alloc]init];
            chosenPicVC.orderID = orderID;

            [self.navigationController pushViewController:chosenPicVC animated:YES];
        }
            break;
        default:
            break;
    }
}

- (void)pushGoReviewControllerWithOrderID:(NSString *)orderID {
    BPSubmitCommentViewController *submitCommentVC = [[BPSubmitCommentViewController alloc]init];
    submitCommentVC.orderID = orderID;
    [self.navigationController pushViewController:submitCommentVC animated:YES];
}




@end
